import uuid,os
from django.db import models
from django.utils import timezone
from apps.Area.models import Area
from op_drf.base import BaseModel
# Create your models here.


class Website(BaseModel):
    '''
    向阳星网站信息
    '''
    name = models.CharField(max_length=255,verbose_name="公司名称",help_text="公司名称",null=False)
    apiurl = models.CharField(max_length=200,verbose_name="接口路径",help_text="接口路径",null=False)
    btn_1 = models.CharField(max_length=20,verbose_name="购药",help_text="购药",null=False,default="医疗诊断")
    btn_2 = models.CharField(max_length=20,verbose_name="诊疗",help_text="诊疗",null=False,default="处方选择")
    btn_3 = models.CharField(max_length=20,verbose_name="配送",help_text="配送",null=False,default="抓药")
    address = models.CharField(max_length=200,verbose_name="公司地址",help_text="公司地址",null=True,blank=True)


    class Meta:
        db_table = "xyx_website"
        verbose_name = "向阳星网站设置"
        verbose_name_plural = verbose_name



class Rad(BaseModel):
    '''
    Rad 服务器
    '''
    name = models.CharField(max_length=20,verbose_name="Rad名称",help_text="Rad名称")
    area_code = models.ForeignKey(
        Area,
        on_delete=models.SET_NULL,
        to_field="d_code",
        null=True,
        blank=True,
        db_constraint=False,
        related_name="rad_area_code",
        verbose_name="所在区域代码",
        help_text="所在区域代码",
    )

    place = models.CharField(max_length=100, verbose_name="地址", null=True, blank=True, help_text="地址")
    # 同步数据时Rad连接信息
    ftp_ip = models.CharField(max_length=20, verbose_name="ftpip", help_text="ftpip", null=True)
    ftp_port = models.IntegerField(verbose_name="ftp端口", help_text="ft端口", null=True)
    ftp_user = models.CharField(max_length=20, verbose_name="ftp用户名", help_text="ftp用户名", null=True)
    ftp_password = models.CharField(max_length=50, verbose_name="ftp密码", help_text="ftp密码", null=True)

    class Meta:
        db_table = "xyx_Rad"
        verbose_name = "Rad服务器信息"
        verbose_name_plural = verbose_name



def files_path(instance, filename):
    return "/".join(
        [
            "system",
            timezone.now().strftime("%Y-%m-%d"),
            str(uuid.uuid4()) + os.path.splitext(filename)[-1],
        ]
    )


class SaveFile(BaseModel):
    name = models.TextField(verbose_name="文件名称", null=True, blank=True)
    type = models.CharField(max_length=200, verbose_name="文件类型", null=True, blank=True)
    size = models.CharField(max_length=64, verbose_name="文件大小", null=True, blank=True)
    address = models.CharField(max_length=16, verbose_name="存储位置", null=True, blank=True)  # 本地、阿里云、腾讯云..
    source = models.CharField(max_length=16, verbose_name="文件来源", null=True, blank=True)  # 导出、用户上传.
    oss_url = models.CharField(max_length=200, verbose_name="OSS地址", null=True, blank=True)
    status = models.BooleanField(default=True, verbose_name="文件是否存在")
    file = models.FileField(
        verbose_name="文件URL",
        upload_to=files_path,
    )
    model = models.CharField(max_length=100, null=True, verbose_name="关联模型")
    relation_id = models.IntegerField(null=True, verbose_name="关联字段")
    model_type = models.CharField(max_length=100, null=True, verbose_name="类型")
    info = models.TextField(verbose_name="文件信息", null=True, blank=True)

    class Meta:
        db_table = "xyx_save_file"
        verbose_name = "文件管理"
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"{self.name}"

class CodeManage(BaseModel):
    """编码"""

    name = models.CharField(max_length=100, verbose_name="名称", null=True, blank=True)
    app = models.CharField(max_length=200, verbose_name="应用", null=True, blank=True)
    model = models.CharField(max_length=200, verbose_name="模型", null=True, blank=True)
    field = models.CharField(max_length=100, verbose_name="字段", null=True, blank=True)
    rule = models.TextField(verbose_name="规则", null=True, blank=True)
    sequence_len = models.IntegerField(verbose_name="序列长度", default=4, blank=True)
    is_active = models.BooleanField(verbose_name="是否启用", default=False)
    anchor_point = models.CharField(max_length=100, verbose_name="键值", null=True, blank=True)

    class Meta:
        db_table = "xyx_code_manage"
        verbose_name = "编码管理"
        verbose_name_plural = verbose_name
